﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using Common.Log;
using Rs.Common.Enum;
using Rs.Common.Model;
using Rs.Common.WepPost;
using WF_Business;

namespace Rs.Common.BLL
{
    public class TaskBLL
    {
        private static DataTable GetData()
        {
            DataTable dtTemp;
            const string strSql = "select appid from xt_appid_iid where issyncdata='0'";
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        private static void SaveData(string appid, SearchSecretOrderResponse model)
        {
            var ida = SysParams.OAConnection(true);
            if (model == null || model.orderDetail == null || model.orderDetail.detailes == null) return;
            try
            {
                var strSql = string.Empty;
                var impDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                model.orderDetail.detailes.ForEach(t =>
                {
                    string strBh = string.IsNullOrEmpty(t.编号) ? t.摄区代号: t.编号;
                    string strDataName = string.IsNullOrEmpty(t.数据名称)?t.摄区名称:t.数据名称;      
                    string strDataScale = string.IsNullOrEmpty(t.比例尺)?t.最小航摄比例尺:t.比例尺;      
                    string strYxsjy = string.IsNullOrEmpty(t.影像数据源)?t.航摄仪:t.影像数据源;   
                    string strImageDate = string.IsNullOrEmpty(t.影像时间)?t.航摄时间:t.影像时间;
                    strBh = string.IsNullOrEmpty(strBh) ? t.路线编号 : strBh;
                    strDataName = string.IsNullOrEmpty(strDataName) ? t.点名 : strDataName;
                    string strCgdymc = string.IsNullOrEmpty(t.成果单元名称) ? t.网名 : t.成果单元名称;
                    strYxsjy = string.IsNullOrEmpty(strYxsjy) ? t.点号 : strYxsjy;
                    string strBbDj = string.IsNullOrEmpty(t.版本) ? t.等级 : t.版本;
                    string strYxscms = string.IsNullOrEmpty(t.影像色彩模式) ? t.等级 : t.影像色彩模式;
                    strYxscms = string.IsNullOrEmpty(strYxscms) ? t.路线名称 : strYxscms;
                    string strScsj = string.IsNullOrEmpty(t.生产时间) ? t.出版时间 : t.生产时间;
                    string strFbl = string.IsNullOrEmpty(t.分辨率) ? t.格网间距 : t.分辨率;                   
                    strSql = string.Format(@"insert into xt_webMapOrderInfo
                    ( id, appid, bh, url, gcjz, newmapno, yxsjy, oldmapno, enterdate, resolution, ffdw, imagedate, shadow, projects, secrets, dataname, ddjz, version, datatype, yxscms, rsname, apptype, scale, impdate)
                     values
                    (seq_ut_ttwid.nextval,'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}', '{13}', '{14}', '{15}', '{16}', '{17}', '{18}', '{19}', '{20}', '{21}', TO_DATE('{22}','yyyy-mm-dd hh24:mi:ss'))",
                     appid, strBh, t.成果数量, t.高程基准, t.新图号, strYxsjy, t.旧图号, strScsj, strFbl, t.分发单位, strImageDate, t.投影, t.所属项目, t.密级, strDataName, t.大地基准, strBbDj, t.数据格式, strYxscms, strCgdymc, t.成果类型, strDataScale, impDate);                   
                    ida.RunSql(strSql);
                });
                strSql = string.Format("update xt_appid_iid set issyncdata='1' where appid='{0}'", appid);
                ida.RunSql(strSql);
                ida.Close(true);
            }
            catch (Exception ex)
            {
                LogManage.addLogErr("SaveData", appid + " " + ex.Message);
                ida.Close(false);
            }
        }

        public static void StartWork()
        {
            var dtInfo = GetData();
            var result = new SearchSecretOrderResponse();
            try
            {
                dtInfo.Rows.Cast<DataRow>().ToList().ForEach(row =>
                {
                    var appid = row["appid"].ToString();
                    var path = ConfigurationManager.AppSettings["apiUrl"];
                    path = string.Concat(path, "&appId=", appid);
                    string respJson;
                    var boolResult = WebPostHelper.Response(string.Empty, path, out respJson);
                    if (!boolResult) return;

                     result = respJson.Json2T<SearchSecretOrderResponse>();
                    if (result.statusCode != EnumResult.成功.EnumToIntStr())
                    {
                        LogManage.addLogErr("StartWork", string.Concat(appid, " ", result.statusCode, " ", result.statusInfo, " ", path));
                        return;
                    }
                    SaveData(appid, result);
                });
            }
            catch (Exception ex)
            {
                LogManage.addLogErr("StartWork", string.Concat(result.statusCode, result.statusInfo, " ", ex.Message));
            }
        }
    }
}
